using System;
using Ext;
using Ext.data;
using Ext.grid;
using ScriptFX;

namespace SampleScripts.grid {
    public class XmlGridScript {
        public static void main(Dictionary args) {
            ExtClass.onReady(new AnonymousDelegate(delegate() { new XmlGridScript().init(); }));
        }

        public void init() {

            // create the Data Store
            Store ds = new Store(new StoreConfig()
                // load using HTTP
                .url("sheldon.xml")

                // the return will be XML, so lets set up a reader
                .reader(new XmlReader(
                    new XmlReaderConfig()
                        // records will have an "Item" tag
                        .record("Item")
                        .id("ASIN")
                        .totalRecords("@total")
                        .ToDictionary(),                     
                    new object[] {
                       // set up the fields mapping into the xml doc
                       // The first needs mapping, the others are very basic
                        new Dictionary("name", "Author", "mapping", "ItemAttributes > Author"),
                        "Title", "Manufacturer", "ProductGroup"
                    }))
                .ToDictionary()
            );

            ColumnModel cm = new ColumnModel(new Dictionary[] {
                new ColumnModelConfig().header("Author").width(120).dataIndex("Author").ToDictionary(),
                new ColumnModelConfig().header("Title").width(180).dataIndex("Title").ToDictionary(),
                new ColumnModelConfig().header("Manufacturer").width(115).dataIndex("Manufacturer").ToDictionary(),
                new ColumnModelConfig().header("Product Group").width(100).dataIndex("ProductGroup").ToDictionary()
            });
            cm.defaultSortable = true;

            // create the grid
            Ext.grid.GridPanel grid = new Ext.grid.GridPanel(new Ext.grid.GridPanelConfig()
                .ds(ds)
                .cm(cm)
                .renderTo("example-grid")
                .width(540)
                .height(200)
                .ToDictionary()
            );

            ds.load();
        }
    }
}
